<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{
    public function index()
    {
        //读取导航数据
        $nav = Db::table('mo_nav')
            ->where('status', 1)
            ->order('sort', 'desc')
            ->select();
        //读取关于我们中的列表
        $about = Db::table('mo_about')
            ->where('status', 1)
            ->order('sort', 'desc')
            ->select();
        //读取核心技术中的列表
        $core = Db::table('mo_core')
            ->where('status', 1)
            ->order('sort', 'desc')
            ->select();
        //读取产品中的列表
        $product = Db::table('mo_product')
            ->where('status', 1)
            ->order('sort', 'desc')
            ->select();
        //读取典型业绩技术中的列表
        $typical = Db::table('mo_typical')
            ->where('status', 1)
            ->order('sort', 'desc')
            ->select();
        //读取联系我们中的列表
        $contact = Db::table('mo_contact')
            ->where('status', 1)
            ->order('sort', 'desc')
            ->select();
        //读取banner列表
        $banner = Db::table('mo_banner')
            ->where('status', 1)
            ->order('sort', 'desc')
            ->select();
        //读取配置信息
        $config = Db::table('mo_config')
            ->where('group', 'index')
            ->select();
        //格式化配置信息 适合前端读取
        $data_config = [];
        if ($config) {
            foreach ($config->toArray() as $item) {
                $data_config[$item['name']] = $item['value'];
            }
        }
//        $this->xx($data_config);
        //给导航子栏目附加子数据
        $data = [];
        if ($nav) {
            foreach ($nav->toArray() as $item) {
                if ($item['tag'] == 'about') {
                    $item['item'] = $about->toArray();
                } elseif ($item['tag'] == 'core') {
                    $item['item'] = $core->toArray();
                } elseif ($item['tag'] == 'product') {
                    $item['item'] = $product->toArray();
                } elseif ($item['tag'] == 'typical') {
                    $item['item'] = $typical->toArray();
                } elseif ($item['tag'] == 'contact') {
                    $item['item'] = $contact->toArray();
                }
                $data[] = $item;
            }
        }

        View::assign([
            'nav' => $data,
            'product' => $product->toArray(),
            'typical' => $typical->toArray(),
            'banner' => $banner->toArray(),
            'config' => $data_config,
            'domain' => 'http://admintest.xlink360.cn'

        ]);
        // 模板输出
        return View::fetch('index');
    }

}
